<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-03-17
 * Time: 9:46
 */
namespace Admin\Controller;

class AdminUserController extends BaseController{

    public  function index(){
        $this->display();
    }

    public  function index_list(){
        $where=array();
        $_where_order_field["alias"]="user";
        $_where_order_field['where']=$where;
        $_where_order_field['id']='order_list_item_content';
        $_where_order_field['field']="user.user_id,user.user_name,user.email,user.add_time,user.last_login";
        $_where_order_field['order']='user_id desc';
        $detail=$this->_getListsFromModel("admin_user",$_where_order_field);
        $this->assign("_dataList",$detail);
        $this->display();
    }


    public  function add(){
        $is_add=I("request.is_add",0);

        if(intval($is_add)==0){
            $this->add_ok();
        }else if(intval($is_add)==1){
            $this->update_ok();
        }
    }

    public  function  add_ok(){
        if(IS_POST){
            $user_name=I("request.user_name");
            $email=I("request.email");
            $password=I("request.passwordok");
            $ec_salt=rand(1,9999);
            $password  = md5(md5($password).$ec_salt);
            $data["user_name"]=$user_name;
            $data["email"]=$email;
            $data["password"]=$password;
            $data["ec_salt"]=$ec_salt;
            $data["add_time"]=time();
            $data['action_list']="无";
            $data["nav_list"]="无";
            $data["agency_id"]=0;
            $groupid=I("request.groupid");
            $rowid=M("admin_user")->data($data)->add();
            if($rowid>0){
                if(!empty($groupid)){
                    foreach($groupid as $key=>$value){
                          $groupdata["uid"]=$rowid;
                          $groupdata["group_id"]=$value;
                          $groupdata["types"]=0;
                         M("auth_group_access")->data($groupdata)->add();
                    }
                }
                $exe_scriptstr = "<script>
                    setTimeout(function(){
                    var win=    art.dialog.opener;
                                win.load();
                                art.dialog.close();
                    },2000);
                   </script>";
                $this->success("添加成功！".$exe_scriptstr);
            }else{
                $this->error("添加失败");
            }
        }else{
            $authgrouplist=M("auth_group")->where(array("types"=>0))->select();
            $this->assign("authgrouplist",$authgrouplist);
            $this->assign("is_add",I("request.is_add"));
            $this->display();
        }
    }


    public  function  update_ok(){
        if(IS_POST){
            $user_id=I("request.user_id");
            $user_name=I("request.user_name");
            $email=I("request.email");
            $password=I("request.passwordok");
            $data["user_name"]=$user_name;
            $data["email"]=$email;
            if(!empty($password)){
                $ec_salt=rand(1,9999);
                $password  = md5(md5($password).$ec_salt);
                $data["password"]=$password;
                $data["ec_salt"]=$ec_salt;
            }
            $groupid=I("request.groupid");
            $rowid=M("admin_user")->where(array("user_id"=>$user_id))->data($data)->save();
            M("auth_group_access")->where(array("uid"=>$user_id))->delete();
            foreach($groupid as $key=>$value){
                $groupdata["uid"]=$user_id;
                $groupdata["group_id"]=$value;
                $groupdata["types"]=0;
                M("auth_group_access")->data($groupdata)->add();
            }


            if($rowid!==false){
                $exe_scriptstr = "<script>
                    setTimeout(function(){
                    var win=    art.dialog.opener;
                                win.load();
                                art.dialog.close();
                    },2000);
                   </script>";
                $this->success("编辑成功！".$exe_scriptstr);
            }else{
                $this->success("编辑失败！");
            }


        }else{
            $userid=I("request.userid");
            $authgrouplist=M("auth_group")->where(array("types"=>0))->select();
            foreach($authgrouplist as $key=>$value){
                $count=M("auth_group_access")->where(array("uid"=>$userid,"group_id"=>$value["id"],"types"=>0))->count();
                if($count>0){
                    $authgrouplist[$key]["istype"]=1;
                }

            }

            $admin_user= M("admin_user")->where(array("user_id"=>$userid))->find();
            $this->assign("m",$admin_user);
            $this->assign("authgrouplist",$authgrouplist);
            $this->assign("is_add",I("request.is_add"));
            $this->display();
        }
    }


    public  function  check(){
         $name=I("request.name");
         $param=I("request.param");
         $msg=I("request.msg");
        if(!empty($name)&&!empty($param)){
              $count=M("admin_user")->where(array($name=>$param))->count();
              if($count>0){
                  $this->ajaxReturn(array("status"=>"n","info"=>$msg."已经存在"));
              }else{
                  $this->ajaxReturn(array("status"=>"y","info"=>"验证通过"));
              }
        }else{
            $this->ajaxReturn(array("status"=>"n","info"=>"没有验证的数据！"));
        }
    }


    public function  delete(){
        $user_id=I("request.user_id");
        if(!empty($user_id)){
              M("auth_group_access")->where(array("uid"=>$user_id,"types"=>0))->delete();
              $rowcount= M("admin_user")->where(array("user_id"=>$user_id))->delete();
              if($rowcount){
                  $this->success("删除成功！");
              }else{
                  $this->error("删除失败！");
              }
        }
    }
}
?>